package com.example.demo.controller;

import com.example.demo.component.RandomComponent;
import com.example.demo.service.redis.IRedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by User on 2017/9/13.
 */
@RestController
public class RedisCacheController {

    private static final Logger log = LoggerFactory.getLogger(RedisCacheController.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RandomComponent randomComponent;

    @Autowired
    private IRedisService redisService;


    @RequestMapping("manualSetCache")
    public void manualSetCache() {
        log.info("manual set cache");

        String cache1 =  randomComponent.nextInt() + "";
        stringRedisTemplate.opsForValue().set("cache1", cache1);
        log.debug("set cache1 = " + cache1);

        String cache2 =  randomComponent.nextInt() + "";
        redisTemplate.opsForValue().set("cache2", cache2);
        log.debug("set cache2 = " + cache2);

    }

    @RequestMapping("manualGetCache")
    public void manualGetCache() {
        log.info("manual get cache");
        String cache1 = stringRedisTemplate.opsForValue().get("cache1");
        log.info("get cache1 = " + cache1);
        Object cache2 = redisTemplate.opsForValue().get("cache2");
        log.info("get cache2 = " + cache2);
    }


}
